package com.emm.yixun.mobile.ui.customer;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.base.BaseActivity;

/**
 * Created by tangxin on 2017/9/22.
 */

public class InvestigationWebView extends BaseActivity {
    String ua = "Mozilla/5.0 (Linux; Android 4.3; Nexus 7 Build/JSS15Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2307.2 Safari/537.360123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199778899090988878";//安全码
    TextView titleMain;
    ImageView backBtn;
    WebView wbInvestigation;
    private SeekBar mSeekBar;//加载进度

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.layout_investigation_activity);
        BaseActivity.activity = this;
        SetRelativiLayoutPadding(R.id.main_head_top_rela);
        titleMain = (TextView) findViewById(R.id.title_main);
        backBtn = (ImageView) findViewById(R.id.back_btn);
        wbInvestigation = (WebView) findViewById(R.id.wb_investigation);
        mSeekBar = (SeekBar) findViewById(R.id.SeekBar);
        initWebSetting();
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        String title = intent.getStringExtra("title");
        titleMain.setText(title);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        wbInvestigation.loadUrl(url);
        WebChromeClient webViewClient = new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                mSeekBar.setProgress(newProgress);
            }
        };
        wbInvestigation.setWebChromeClient(webViewClient);
        mSeekBar.setOnSeekBarChangeListener(new android.widget.SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress >= 100) {// 进度加载完成,进度条隐藏
                    titleMain.setText(wbInvestigation.getTitle());
                    mSeekBar.setVisibility(View.GONE);
                } else {
                    mSeekBar.setVisibility(View.VISIBLE);
                }
            }
        });
        wbInvestigation.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebSetting() {
        wbInvestigation.getSettings().setSupportZoom(false);
        wbInvestigation.getSettings().setDatabaseEnabled(true);
        wbInvestigation.getSettings().setBlockNetworkImage(false);
        // 设置安全编码
        wbInvestigation.getSettings().setUserAgentString(ua);
        wbInvestigation.getSettings().setJavaScriptEnabled(true);
        // 去掉缩放按钮
        wbInvestigation.getSettings().setBuiltInZoomControls(true);
        wbInvestigation.getSettings().setDisplayZoomControls(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            wbInvestigation.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        wbInvestigation.getSettings().setAppCacheEnabled(true);
        //设置 缓存模式
        wbInvestigation.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        wbInvestigation.getSettings().setDomStorageEnabled(true);

        wbInvestigation.addJavascriptInterface(new OpenApp(InvestigationWebView.this) {
            @Override
            public void canBackReLoad() {
                if (wbInvestigation.canGoBack()) {
                    wbInvestigation.goBack();
                }
            }
        }, "openApp");
    }

    @Override
    public void onBackPressed() {
        if (wbInvestigation.canGoBack()) {
            wbInvestigation.goBack();
        } else {
            super.onBackPressed();
        }
    }

}
